﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Dapper.Moon
{
    public class AopProvider : IAop
    {
        /// <summary>
        /// 执行前
        /// </summary>
        public Action<ExecutingArgs> OnExecuting { get; set; }
        /// <summary>
        /// 执行后
        /// </summary>
        public Action<ExecutedArgs> OnExecuted { get; set; }
    }

    public class ExecuteBaseArgs
    {
        public string LogId { get; set; }
        public ExecuteType ExecuteType { get; set; }
    }

    /// <summary>
    /// 执行前参数
    /// </summary>
    public class ExecutingArgs : ExecuteBaseArgs
    {
        public ExecutingArgs(string sql, DynamicParameters parameters)
        {
            this.Sql = sql;
            this.DynamicParameters = parameters;
            this.Stopwatch = new System.Diagnostics.Stopwatch();
            this.Stopwatch.Start();
        }

        public System.Diagnostics.Stopwatch Stopwatch { get; private set; }
        /// <summary>
        /// sql语句
        /// </summary>
        public string Sql { get; private set; }
        /// <summary>
        /// sql参数 可使用RawString方法查看参数名称和值
        /// </summary>
        public DynamicParameters DynamicParameters { get; private set; }
    }

    /// <summary>
    /// 执行后参数
    /// </summary>
    public class ExecutedArgs : ExecuteBaseArgs
    {
        public ExecutedArgs(ExecutingArgs executingArgs, Exception exception, object executeResult)
        {
            if (executingArgs != null)
            {
                executingArgs.Stopwatch.Stop();
                ElapsedMilliseconds = executingArgs.Stopwatch.ElapsedMilliseconds;
            }
            this.Exception = exception;
            this.ExecuteResult = executeResult;
        }

        public Exception Exception { get; private set; }
        public object ExecuteResult { get; private set; }
        public long ElapsedMilliseconds { get; private set; }
    }

    public enum ExecuteType
    {
        Query = 0,
        NonQuery = 1
    }
}
